package com.orange.delivery.presentation;

import java.util.Properties;

import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.apache.wicket.settings.IExceptionSettings;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.francetelecom.clara.component.wicket.app.utils.OrangeUtilities;
import com.orange.delivery.presentation.exception.AccessDeniedPage;
import com.orange.delivery.presentation.exception.InternalErrorPage;
import com.orange.delivery.presentation.exception.PageExpiredErrorPage;
import com.orange.delivery.presentation.page.HomePage;
import com.orange.delivery.presentation.page.LoginPage;
import com.orange.delivery.presentation.page.LogoutPage;
import com.orange.delivery.presentation.page.MarketPage;

/**
 * Entry point for wicket application. Configure general application settings and define application session
 * 
 */
public class WicketApplication extends AuthenticatedWebApplication {
	private static final Logger LOG = LoggerFactory.getLogger(WicketApplication.class.getName());

	private String configuration;

	/** dependency injection */
	public void setConfiguration(String configuration) {
		this.configuration = configuration;
	}

	@Override
	public RuntimeConfigurationType getConfigurationType() {
		if (configuration == null) {
			return RuntimeConfigurationType.DEPLOYMENT;
		} else {
			return RuntimeConfigurationType.valueOf(configuration.toUpperCase());
		}
	}

	/**
	 * The application home page
	 * 
	 * @see com.orange.delivery.presentation.page.HomePage
	 */
	@Override
	public Class<HomePage> getHomePage() {
		return com.orange.delivery.presentation.page.HomePage.class;
	}

	/**
	 * Define application settings
	 */
	@Override
	protected void init() {
		LOG.debug("wicket application init method");

		super.init();

		// for @SpringBean annotation
		defineSpringInjector();

		// change default error/timeout pages in production mode
		defineErrorPage();

		if (!RuntimeConfigurationType.DEVELOPMENT.equals(getConfigurationType())) {
			// remove "wicket:id" from html generated code in production mode
			getMarkupSettings().setStripWicketTags(true);
		}

		// assign standard URLs to bookmarkable pages
		mountPages();

		// for library of component
		getJavaScriptLibrarySettings()
				.setJQueryReference(new JavaScriptResourceReference(OrangeUtilities.class, "jquery-1.7.1.js"));

		getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
	}

	private void mountPages() {
		mountPage("login", LoginPage.class);
		mountPage("logout", LogoutPage.class);
		mountPage("denied", AccessDeniedPage.class);
		mountPage("error", InternalErrorPage.class);
		mountPage("expired", PageExpiredErrorPage.class);

		mountPage("homepage", HomePage.class);
		mountPage("market", MarketPage.class);
	}

	protected void defineSpringInjector() {
		// SpringComponentInjector uses Spring's WebApplicationContextUtils
		// class to retrieve
		// Spring context (loaded from web.xml file).
		getComponentInstantiationListeners().add(new SpringComponentInjector(this));
	}

	protected void defineErrorPage() {
		getApplicationSettings().setPageExpiredErrorPage(PageExpiredErrorPage.class);
		getApplicationSettings().setAccessDeniedPage(AccessDeniedPage.class);
		getApplicationSettings().setInternalErrorPage(InternalErrorPage.class);
		getExceptionSettings().setUnexpectedExceptionDisplay(IExceptionSettings.SHOW_INTERNAL_ERROR_PAGE);
	}

	// getWebSessionClass and getSignInPageClass must be overrriden
	@Override
	protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
		return WicketSession.class;
	}

	@Override
	protected Class<? extends WebPage> getSignInPageClass() {
		Properties applicationProperties = WicketSession.get().getApplicationProperties();
		if (applicationProperties != null
				&& "authentication_form".equals(applicationProperties.getProperty("spring.profiles.active"))) {
			LOG.debug("redirecting to login page");
			return LoginPage.class;
		} else {
			LOG.debug("redirecting to denied page");
			return AccessDeniedPage.class;

		}
	}
}
